/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.membermsg.remote.RemoteMemberMsgServiceImpl
   Module Description   :

   Date Created      : 2011/1/21
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.membermsg.remote;

import java.util.Date;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.ec.membermsg.entity.MemberMsg;
import com.yaodian100.ec.membermsg.service.MemberMsgService;

/**
 * @author cut7man
 * 
 */
public class RemoteMemberMsgServiceImpl implements RemoteMemberMsgService {
	@Resource
	private BaseManager baseMgr;
	@Resource
	private MemberMsgService msgService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.membermsg.remote.RemoteMemberMsgService#moveMemberMsg(java.lang.Integer)
	 */
	@Override
	public String moveMemberMsg(Integer batchSize) {
		DetachedCriteria dc = DetachedCriteria.forClass(MemberMsg.class);
		Date currentDate = new Date();
		dc.add(Restrictions.eq("status", MemberMsg.STATUS.WAIT_FOR_SEND.getValue()));
		dc.add(Restrictions.le("sendTime", currentDate));
		Object[] results = baseMgr.findByCriteria(dc);

		for (Object obj : results) {
			MemberMsg msg = (MemberMsg) obj;

			MemberMsg currentMsg = (MemberMsg) baseMgr.get(MemberMsg.class, msg.getId());
			if (!currentMsg.getStatus().equals(MemberMsg.STATUS.WAIT_FOR_SEND.getValue())) {
				// 若状态不为待发送，skip
				continue;
			}

			msgService.moveMsg(currentMsg);

		}
		return "true";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.membermsg.remote.RemoteMemberMsgService#sendNotDeliverMemberMsg(java.lang.Integer)
	 */
	@Override
	public String sendNotDeliverMemberMsg(String orderId, String productIds) {
		try {
			msgService.sendNotDeliverMsg(orderId, productIds);
		} catch (Exception e) {
			String execLog = StringUtils.printExecption(null, e);
			return execLog;
		}
		return "true";
	}
}
